package xx.services.demo.rest;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.demo.DemoTab;
import xx.entity.demo.DemoTabItem;
import xx.services.Passport;
import xx.services.demo.inter.ITabInterface;
import xx.services.util.Constants;
import xx.util.Arr;
import xx.util.string.Format;

/**
 * DEMO Tab模板服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/demo/tab")
 * @author Administrator
 * 
 * 2010-12-20
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/demo/tab")
public class TabRest extends Passport implements ITabInterface
{

    public TabRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 新增一个标签demo。<br>
     * 
     * @POST
     * 
     * @param title-@FormParam("title")
     *            Tab标题
     * @param container-@FormParam("container")
     *            Tab容器，一般是一个div，如：<div class="container">${container}</div>
     * @param demo-@FormParam("demo")
     *            dome样例
     * @param shape-@FormParam("shape")
     *            样式图片
     * @param description-@FormParam("description")
     *            描述
     * @return 新Tab实体
     * @author chenxiang
     */
    @POST
    public String add(@FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("demo")
    String demo, @FormParam("shape")
    String shape, @FormParam("description")
    String description)
    {
        try
        {
            DemoTab dt = new DemoTab(qr, owner);
            dt.setTitle(title).setContainer(container).setDemo(demo).setShape(shape);
            dt.setDescription(description);
            dt.insert();
            return ok(dt);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 新增一个TabItem tab_item demo。<br>
     * 
     * @POST
     * @Path ("/{tab_id}/item")
     * 
     * @param tabId-@PathParam("tab_id")
     *            Tab ID
     * @param title-@FormParam("title")
     *            item标题
     * @param container-@FormParam("container")
     *            item容器，一般是一个div，如：<div class="container">${container}</div>
     * @param demo-@FormParam("demo")
     *            Item实体
     * @param templet-@FormParam("templet")
     *            Item模板
     * @return 新TabItem实体
     * @author chenxiang
     */
    @POST
    @Path("/{tab_id}/item")
    public String addItem(@PathParam("tab_id")
    long tabId, @FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("demo")
    String demo, @FormParam("templet")
    String templet)
    {
        try
        {
            DemoTabItem dti = new DemoTabItem(qr, owner);
            dti.setTabId(tabId).setTitle(title).setContainer(container).setDemo(demo).setTemplet(templet);
            dti.insert();
            return ok(dti);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 删除Tab。<br>
     * 
     * @DELETE
     * @Path ("/{tab_id}")
     * 
     * @param tabId-@PathParam("tab_id")
     *            Tab ID，多个ID用“_”隔开
     * @return true-删除成功，string-其他删除失败（string就是失败原因）
     * @author chenxiang
     */
    @DELETE
    @Path("/{tab_id}")
    public String delete(@PathParam("tab_id")
    String tabId)
    {
        try
        {
            int count = 0;
            long[] tabIdArr = Arr.toLongArray(tabId, "_");
            for (long id : tabIdArr)
            {
                DemoTab dt = new DemoTab(qr, owner);
                dt.setTabId(id);
                dt.delete();
                count++;
            }
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 获取一个标签demo。<br>
     * 
     * @GET
     * @Path ("/{tab_id}")
     * 
     * @param tabId-@PathParam("tab_id")
     *            Tab ID
     * @return Tab实体
     * @author chenxiang
     */
    @GET
    @Path("/{tab_id}")
    public String get(@PathParam("tab_id")
    long tabId)
    {
        try
        {
            DemoTab dt = new DemoTab(qr, owner);
            dt.setTabId(tabId).load();
            return ok(dt);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 描述：获取标签列tab_item列表。<br>
     * 
     * @GET
     * @Path ("/{tab_id}/item")
     * 
     * @param tabId-@PathParam("tab_id")
     *            Tab ID
     * @return item列表
     * @author chenxiang
     */
    @GET
    @Path("/{tab_id}/item")
    public String getItems(@PathParam("tab_id")
    long tabId)
    {
        try
        {
            DemoTabItem dti = new DemoTabItem(qr, owner);
            StringBuilder cond = new StringBuilder();
            cond.append(DemoTabItem.TAB_ID).append("=").append(tabId);
            
            if (!Format.isEmpty(condition))
            {
                cond.append(cond.length() > 0 ? " AND " : "").append("(").append(condition).append(
                        ")");
            }
            
            RowSet rs = dti.getList(pageSize, pageNo, cond.toString(), orderField, fields);
            Map<String, Object> map = new HashMap<String, Object>();
            if (isTotal)
            {
                DemoTab dt = new DemoTab(qr, owner);
                map.put(Constants.TOTAL, dt.getTabItemCount(cond.toString()));
            }
            return ok(rs, map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 获取标签列表。<br>
     * 
     * @GET
     * 
     * @return Tab实体列表
     * @author chenxiang
     */
    @GET
    public String gets()
    {
        try
        {
            DemoTab dt = new DemoTab(qr, owner);
            RowSet rs = dt.getList(pageSize, pageNo, condition, orderField, fields);
            Map<String, Object> map = new HashMap<String, Object>();
            if (isTotal)
            {
                map.put(Constants.TOTAL, dt.getTabCount(condition));
            }
            return ok(dt.rsToList(rs), map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 修改一个标签demo。<br>
     * 
     * @PUT
     * @Path ("/{tab_id}")
     * 
     * @param tabId-@PathParam("tab_id")
     *            Tab ID
     * @param title-@FormParam("title")
     *            Tab标题
     * @param container-@FormParam("container")
     *            Tab容器，一般是一个div，如：<div class="container">${container}</div>
     * @param demo-@FormParam("demo")
     *            dome样例
     * @param shape-@FormParam("shape")
     *            样式图片
     * @param description-@FormParam("description")
     *            描述
     * @return 新Tab实体
     * @author chenxiang
     */
    @PUT
    @Path("/{tab_id}")
    public String update(@PathParam("tab_id")
    long tabId, @FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("demo")
    String demo, @FormParam("shape")
    String shape, @FormParam("description")
    String description)
    {
        try
        {
            DemoTab dt = new DemoTab(qr, owner);
            dt.setTabId(tabId);
            StringBuilder condition = new StringBuilder();
            if (title != null && !"".equals(title.trim()))
            {
                condition.append("title,");
                dt.setTitle(title);
            }
            if (container != null && !"".equals(container.trim()))
            {
                condition.append("container,");
                dt.setContainer(container);
            }
            if (demo != null && !"".equals(demo.trim()))
            {
                condition.append("demo,");
                dt.setDemo(demo);
            }
            if (shape != null && !"".equals(shape.trim()))
            {
                condition.append("shape,");
                dt.setShape(shape);
            }
            if (description != null && !"".equals(description.trim()))
            {
                condition.append("description,");
                dt.setDescription(description);
            }

            if (condition.length() > 0)
            {
                dt.update(condition.toString());
            }
            else
            {
                dt.update();
            }
            return ok(dt);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }
}
